/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.model.featureGenerator.jocl;

import avl.sv.shared.model.featureGenerator.AbstractFeatureGenerator;
import avl.sv.shared.model.featureGenerator.FeatureGeneratorOptionsPromptInterface;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author benbryan
 */
public class OptionsPromptJOCL_Features extends javax.swing.JPanel implements FeatureGeneratorOptionsPromptInterface {

    @Override
    public AbstractFeatureGenerator getFeatureGenerator() {
        FeatureGeneratorJOCL out = new FeatureGeneratorJOCL();
        out.planes = planesSelected;
        return out;
    }
    private final ArrayList<Plane> planesSelected = new ArrayList<>();
    private final ArrayList<Plane> planesAll;
    private class FeatureTableModel extends AbstractTableModel {

        private final int numelRows = Feature.Names.values().length;
        private final int numelColumns = Plane.Names.values().length + 1;

        public FeatureTableModel() {
            for (Plane.Names planeName:Plane.Names.values()){
                ArrayList<Feature> features = new ArrayList<>();
                for (Feature.Names featureName:Feature.Names.values()){
                    Feature feature = new Feature(featureName);
                    features.add(feature);
                }
                planesAll.add(new Plane(planeName, features));
            }
        }       

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if (columnIndex > 0) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public String getColumnName(int columnIndex) {
            if (columnIndex > 0) {
                return planesAll.get(columnIndex-1).planeName.name();
            } else {
                return "Name";
            }
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex > 0) {
                return Boolean.class;
            } else {
                return String.class;
            }
        }

        @Override
        public int getRowCount() {
            return numelRows;
        }

        @Override
        public int getColumnCount() {
            return numelColumns;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0){
                return Feature.Names.values()[rowIndex].name();
            } else {
                Plane plane = planesAll.get(columnIndex-1);
                Feature feature = plane.features.get(rowIndex);
                for (Plane p:planesSelected){
                    if (p.planeName.equals(plane.planeName)){
                        for (Feature f:p.features){
                            if (f.featureName.equals(feature.featureName)){
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0){
                return;
            } else {
                if (aValue instanceof Boolean){
                    boolean isChecked = (boolean) aValue;
                    if (isChecked){
                        Plane plane = planesAll.get(columnIndex-1);
                        Feature feature = plane.features.get(rowIndex);
                        for (Plane p:planesSelected){
                            if (p.planeName.equals(plane.planeName)){
                                for (Feature f:p.features){
                                    if (f.featureName.equals(feature.featureName)){
                                        return;
                                    }
                                }
                                p.features.add(feature);
                                return;
                            }
                        }
                        ArrayList<Feature> newFeature = new ArrayList<>();
                        newFeature.add(feature);
                        planesSelected.add(new Plane(plane.planeName, newFeature));
                    } else {
                        Plane plane = planesAll.get(columnIndex-1);
                        Feature feature = plane.features.get(rowIndex);
                        for (Plane p:planesSelected){
                            if (p.planeName.equals(plane.planeName)){
                                for (Feature f:p.features){
                                    if (f.featureName.equals(feature.featureName)){
                                        p.features.remove(f);
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
                
            }
        }
    };

    public OptionsPromptJOCL_Features(FeatureGeneratorJOCL jocl_Features) {
        initComponents();
        planesSelected.addAll(jocl_Features.planes);
        planesAll = new ArrayList<>();
        jTableFeatures.setModel(new FeatureTableModel());
        TableColumnModel cm = jTableFeatures.getColumnModel();
        cm.getColumn(0).setPreferredWidth(300);
        for (int i = 1; i < cm.getColumnCount(); i++){
            cm.getColumn(i).setPreferredWidth(150);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableFeatures = new javax.swing.JTable();

        jTableFeatures.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTableFeatures.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTableFeatures);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableFeatures;
    // End of variables declaration//GEN-END:variables

}
